﻿using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using PodpisyElektorniczne.Classes;


namespace PodpisyElektorniczne.Interfaces
{
    public class LoggedIn
    {
        private User usr;
        private SqlConnection connection;
        private SqlCommand cmd;
        //public static string conString = @"Data Source=KAMIL-KOMPUTER;Initial Catalog=RingSignature;Integrated Security=True";KAMIL-PC
        public static string conString = @"Data Source=KAMIL-PC;Initial Catalog=RingSignatures;Integrated Security=True";
        private bool state;

        public User Usser{get { return usr; }set { usr = value; }}
        //KAMIL-KOMPUTER (Kamil-Komputer\Kamil)
        public LoggedIn()
        {
            usr = new User();
            state = false;
            connection = new SqlConnection(conString);
            
        }

        public bool TryToRegister(string login, string name, string surname, string email, string pass)
        {
            bool isRegister = false;
            if(connection == null)
                connection = new SqlConnection(conString);
            cmd = new SqlCommand();
            cmd.Connection = connection;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "AddUser";

            SqlParameter pLog = cmd.Parameters.Add(new SqlParameter("@login", SqlDbType.NVarChar, 50));
            pLog.Value = login;
            SqlParameter pName = cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar, 50));
            pName.Value = name;
            SqlParameter pSurname = cmd.Parameters.Add(new SqlParameter("@surname", SqlDbType.NVarChar, 150));
            pSurname.Value = surname;
            SqlParameter pEmail = cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.NVarChar, 250));
            pEmail.Value = email;
            SqlParameter pPass = cmd.Parameters.Add(new SqlParameter("@pass", SqlDbType.NVarChar, 50));
            pPass.Value = pass;

            connection.Open();
            using(SqlDataReader rdr = cmd.ExecuteReader())
            {
                while(rdr.Read())
                {
                    if(rdr.FieldCount > 0)
                    {
                        if (rdr[0].ToString() == "0")
                            isRegister = true;
                        else
                        {
                            MessageBox.Show(@"This users exists in database, change data and try again.", "Error",MessageBoxButtons.OK);
                        }
                    }
                }
            }
            connection.Close();
            cmd.Dispose();
            return isRegister;
        }

        public bool TryToLogin(string login, string pass)
        {

            try
            {
                cmd = new SqlCommand();
                cmd.CommandText = "LogInProc";
                cmd.Connection = connection;
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter pLog = cmd.Parameters.Add(new SqlParameter("@login", SqlDbType.NVarChar, 50));
                pLog.Value = login;
                SqlParameter pPass = cmd.Parameters.Add(new SqlParameter("@pass", SqlDbType.NVarChar, 50));
                pPass.Value = pass;
                connection.Open();
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        if (rdr[0].ToString() == "-1")
                        {
                            MessageBox.Show(@"Login or password is incorrect. Try again.", @"Error",
                                            MessageBoxButtons.OK);

                        }

                        else
                        {
                            usr.Name = rdr["Name"].ToString();
                            usr.Surname = rdr["Surname"].ToString();
                            usr.Pass = pass;
                            usr.Login = login;
                            state = true;
                            break;
                        }
                    }
                }
                connection.Close();
                if (state)
                {
                    connection.Dispose();

                }
                cmd.Dispose();
                return state;
            }
            catch(ConstraintException ex)
            {
                throw ex;
            }
        }

    }
}
